package com.basics.framework.jwt;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.basics.common.utils.GuidUtils;

import java.util.Date;

public class JwtUtil {
    public static final long EXPIRE_TIME = 86400000L;
    private static final String SECRET = "a98516bb92254a67";

    public JwtUtil() {
    }

    public static String sign(String userId) {
        try {
            Date date = new Date(System.currentTimeMillis() + 86400000L);
            Algorithm algorithm = Algorithm.HMAC256("a98516bb92254a67");
            return JWT.create().withAudience(new String[]{userId}).withExpiresAt(date).withClaim("rollNum", GuidUtils.generateSimpleGuid()).sign(algorithm);
        } catch (Exception var3) {
            return null;
        }
    }

    public static String getUserId(String token) {
        try {
            String userId = (String)JWT.decode(token).getAudience().get(0);
            return userId;
        } catch (JWTDecodeException var2) {
            return null;
        }
    }

    public static boolean checkSign(String token) {
        try {
            Algorithm algorithm = Algorithm.HMAC256("a98516bb92254a67");
            JWTVerifier verifier = JWT.require(algorithm).build();
            verifier.verify(token);
            return true;
        } catch (JWTVerificationException var3) {
            throw new RuntimeException("token 无效，请重新获取");
        }
    }
}
